package com.sm.controller;

import com.sm.bo.VipActivitiesOrderBo;
import com.sm.bo.VipOrderBO;
import com.sm.dao.ActivitiesMapper;
import com.sm.model.Activities;
import com.sm.model.User;
import com.sm.model.UserActivities;
import com.sm.model.VipDictionary;
import com.sm.service.*;
import com.sm.util.Msg;
import com.sm.wx.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/vipActivitiesOrder")
public class VipActivitiesOrderController {

    @Autowired
    private VipDictionaryService vipDictionaryService;
    @Autowired
    private UserService userService;
    @Autowired
    private VipActivitiesOrderService vipActivitiesOrderService;
    @Autowired
    private ActivitiesService activitiesService;
    @Autowired
    private UserActivitiesService userActivitiesService;

    /**
     * 创建参会券订单
     *
     * @param vipOrder
     * @return
     */
    @RequestMapping("/create")
    @ResponseBody
    public Msg createOrder(HttpServletRequest request, VipActivitiesOrderBo vipOrder) {
        String msg;
        User user = userService.selectUserById(vipOrder.getUserId());

        //应该是多少钱
        if (vipOrder.getUserId() == null || user == null) {
            msg = "用户不存在";
            return Msg.fail().add("msg", msg);
        } else if (vipOrder.getPayType() != 1 && vipOrder.getPayType() != 2) {
            msg = "请选择支付方式";
            return Msg.fail().add("msg", msg);
        }
        Activities activities = activitiesService.selectActivitiesById(vipOrder.getActivitiesId());
        if (new BigDecimal(activities.getActivitiesPrice()).compareTo(vipOrder.getPrice()) != 0) {
            msg = "价格异常";
        } else  {
            AjaxResult order = vipActivitiesOrderService.createVipOrder(request, vipOrder);
            return Msg.success().add("order", order);
        }
        return Msg.fail().add("msg", msg);
    }


}
